//
//  Kylin_SeeBigPictureViewController.m
//  03-BuDeJie
//
//  Created by Kylin on 16/3/6.
//  Copyright © 2016年 kylin. All rights reserved.
//

#import "Kylin_SeeBigPictureViewController.h"
#import "Kylin_TopicItem.h"
#import <UIImageView+WebCache.h>
#import <SVProgressHUD.h>

@interface Kylin_SeeBigPictureViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *saveButton;
/**显示片的View*/
@property(nonatomic,weak)UIImageView *imageView;

@end

@implementation Kylin_SeeBigPictureViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.frame = [UIScreen mainScreen].bounds;
    [scrollView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(back)]];
    
    // 选择添加的位置,让添加的view在最里面,不挡住其它按钮
    [self.view insertSubview:scrollView atIndex:0];
    
    // imageView
    UIImageView *imageView = [[UIImageView alloc]init];
    CGFloat imageW = Kylin_ScreenW;
    CGFloat imageH = imageW * self.topicItem.height / self.topicItem.width; // 等比例绽放
    CGFloat imageY = 0;
    if (imageH < Kylin_ScreenH) {
        imageY = (Kylin_ScreenH - imageH) * 0.5; // 上下留一样的距离
    }else{// 图片超过屏幕时,让scrollView的滚动范围跟着图片高度
        scrollView.contentSize = CGSizeMake(0, imageH);
    }
    imageView.frame = CGRectMake(0, imageY, imageW, imageH);
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.topicItem.image1] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (image == nil) return ;
        self.saveButton.enabled = YES;
    }];
    
    [scrollView addSubview:imageView];
    self.imageView = imageView;
    
    // 缩放
    CGFloat maxScale = self.topicItem.width / imageW;
    if (maxScale > 1.0) {
        scrollView.maximumZoomScale = maxScale;
        scrollView.delegate = self;
    }
}

#pragma mark- UIScrollViewDelegate
// 该方法告诉view显示在哪个地方,设置后显示在scrollView中里面的View中
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
   return self.imageView;
}

#pragma mark- 点击事件处理
- (IBAction)saveButtonClick {
    
  [self.imageView.image kylin_saveToCustomAlbumWithCompletionHandler:^(BOOL success, NSError * error) {
      
     dispatch_sync(dispatch_get_main_queue(), ^{
         if (success) {
             [SVProgressHUD showSuccessWithStatus:@"保存完成"];
         }else{
             [SVProgressHUD showErrorWithStatus:@"保存失败"];
         }
     });
   
  }];
    
}

// 保存到相机胶卷 方法
//    UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
// 该方法与上面的方法需配套使用
//- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
//    
//    if (error) {
//        [SVProgressHUD showErrorWithStatus:@"保存失败"];
//    }else{
//        [SVProgressHUD showSuccessWithStatus:@"保存成功"];
//    }
//}
- (IBAction)cancelButtonClick {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)back{
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
